function login_submit() {
	const doctor_id = document.getElementById('account-InputBlank').value;
	const password = document.getElementById('password-InputBlank').value;

	if (!doctor_id || !password) {
		showError('请输入用户名和密码');
		return;
	}

	fetch('/login', {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			doctor_id: doctor_id,
			password: password
		}),
		credentials: 'same-origin'
	})
	.then(response => {
		if (!response.ok) {
			return response.json().then(data => {
				throw new Error(data.error || '登录失败');
			});
		}
		return response.json();
	})
	.then(data => {
		if (data.msg === 'success') {
			const urlParams = new URLSearchParams(window.location.search);
			const nextUrl = urlParams.get('next') || '/';
			window.location.href = nextUrl;
		} else {
			showError(data.error || '登录失败');
		}
	})
	.catch(error => {
		console.error('Error:', error);
		showError(error.message || '网络错误，请稍后重试');
	});
}

function logout() {
	if (confirm('确定要退出登录吗？')) {
		fetch('/auth/logout', {
			method: 'POST',
			credentials: 'same-origin'
		})
		.then(response => {
			if (response.ok) {
				window.location.href = '/login';
			} else {
				showError('退出失败，请稍后重试');
			}
		})
		.catch(error => {
			console.error('Error:', error);
			showError('网络错误，请稍后重试');
		});
	}
}

function showError(message) {
	const errorDiv = document.getElementById('error_information');
	const alertModal = document.getElementById('alert');
	if (errorDiv && alertModal) {
		errorDiv.textContent = message;
		alertModal.style.display = 'flex';
	}
}

function removeAlert() {
	const alertModal = document.getElementById('alert');
	if (alertModal) {
		alertModal.style.display = 'none';
	}
}
